/**
 * 
 */
package com.itinsys.enma.manager.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.itinsys.enma.dao.CareerDAO;
import com.itinsys.enma.domain.Career;
import com.itinsys.enma.domain.Subject;
import com.itinsys.enma.domain.response.CareerResponse;
import com.itinsys.enma.domain.response.EnmaSubjectResponse;
import com.itinsys.enma.domain.response.SubjectResponse;
import com.itinsys.enma.manager.CareerManager;
import com.itinsys.enma.manager.ResponseManager;
import com.itinsys.enma.manager.SchoolManager;

/**
 * @author rperez
 *
 */
public class CareerManagerImpl implements CareerManager{

	@Autowired
	private CareerDAO careerDAO;
	@Autowired
	private SchoolManager schoolManager;
	@Autowired
	private ResponseManager responseManager;
	
	@Override
	public CareerResponse getAllCareers() {
		CareerResponse response = new CareerResponse();
		List<Career> career = careerDAO.getAll();
		if(career.isEmpty())
			responseManager.prepareResponse(response, CareerResponse.CAREER_NOT_FOUND);
		else{
			responseManager.prepareResponse(response, CareerResponse.OK);
			response.setCareers(career);
		}
		return response;
	}


	@Override
	public CareerResponse addCareer(Career career) {
		
		CareerResponse response = new CareerResponse();
			if(careerDAO.addCareer(career))		
				response=getAllCareers();
			else
				responseManager.prepareResponse(response, CareerResponse.ERROR);
		return response;
	}

	@Override
	public CareerResponse updateCareer(Career career) {
		CareerResponse response = new CareerResponse();
		Career careerToUpdate = careerDAO.getById(career.getId());
		
		careerDAO.updatePlanningCareerNameOnMemory(careerToUpdate.getDescription(),career.getDescription());
		
		careerToUpdate.setDescription(career.getDescription());
		careerToUpdate.setSchool(schoolManager.getSchoolById(career.getSchool().getId()));
		
		if(careerDAO.updateCareer(careerToUpdate))		
			response=getAllCareers();
		else
			responseManager.prepareResponse(response, CareerResponse.ERROR);

		return response;
	}
	
	@Override
	public EnmaSubjectResponse addSubject(Career career,Subject aSubject) {
		EnmaSubjectResponse response = new EnmaSubjectResponse();
		career.getSubjects().add(aSubject);
		
		if(careerDAO.updateCareer(career))		
			response=getAllSubjectsByCareer(career.getId());
		else
			responseManager.prepareResponse(response, CareerResponse.ERROR);

		return response;
	}

	
	@Override
	public CareerResponse deleteCareer(Career career) {
		CareerResponse response = new CareerResponse();
		
		career = careerDAO.getById(career.getId());
		if(careerDAO.deleteCareer(career))		
			response = getAllCareers();
		else
			responseManager.prepareResponse(response, CareerResponse.ERROR);
		
		return response;
	}

	
	@Override
	public EnmaSubjectResponse getAllSubjectsByCareer(Integer idCareer) {
		EnmaSubjectResponse response = new EnmaSubjectResponse();
		List<Subject> careerSubjects = careerDAO.getAllSubjectsByCareer(idCareer);
		if(careerSubjects == null)
			responseManager.prepareResponse(response, SubjectResponse.ERROR);
		else{
			responseManager.prepareResponse(response, CareerResponse.OK);		
			response.setSubjects(careerSubjects);
		}
		return response;
	}

	@Override
	public Career getCareeById(int careerId) {
		return careerDAO.getById(careerId);
	}

	
}
